package com.googlecode.connectlet.machine.provider.key;

import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGeneratorSpi;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import com.googlecode.connectlet.machine.provider.MachineKey;

public final class MachineKeyPairGenerator extends KeyPairGeneratorSpi {
	private MachineKey key;
	private int keySize;

	@Override
	public KeyPair generateKeyPair() {
		return key.generateKeyPair(keySize);
	}

	@Deprecated
	@Override
	public void initialize(int keysize, SecureRandom random) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
		if (params instanceof MachineKey) {
			key = (MachineKey) params;
			if (params instanceof MachineKeyWithSize) {
				keySize = ((MachineKeyWithSize) params).getKeySize();
			} else {
				keySize = 2048;
			}
		} else {
			throw new InvalidAlgorithmParameterException("MachineKey required");
		}
	}
}